home *** CD-ROM | disk | FTP | other *** search
- <?php
- /* thumbnail.php
- * .net magazine (www.netmag.co.uk), issue 83
- * Matt Kynaston, 2001
- * Distributed under the GNU Public License - www.gnu.org/copyleft/gpl.html
- *
- * usage: thumbnail.php?file=filename.jpg
- * This file outputs a thumnail image and from the file passed to it
- * in the 'file' query string.
- * It uses the GD extension to manipulate the images - the current versions
- * can't handle GIF images for licensing reasons: check which version your
- * host has installed before using it.
- * If there is an error, or the file type is wrong, it generates a
- * 'no thumbnail available' image instead
- *
- * For more information on the image manipulation functions this script
- * uses - and many more besides - see 'Image Functions' in the PHP Manual's
- * Function Reference.
- */
-
- /* The next line tells the browser the image type being returned. Note
- * that php scripts can return nearly any kind of file (images, wap pages,
- * PDF documents, etc.) - so long as the correct content type is specified
- */
- Header('Content-type: image/png');
-
- /* GetImageSize returns an array:
- * $info[0] - horizontal size of image in pixels
- * $info[1] - vertical size of image in pixels
- * $info[2] - type of image (1=GIF, 2=JPEG, 3=PNG)
- */
- $info = GetImageSize("$file");
-
-
- // is image big enough to need resizing?
- if ($info[0] > MAX_XY || $info[1] > MAX_XY) {
- // is image landscape?
- if ($info[0] >= $info[1]) {
- $width = MAX_XY;
- $height = $info[1]*MAX_XY/$info[0];
- // or portrait?
- } else {
- $height = MAX_XY;
- $width = $info[0]*MAX_XY/$info[1];
- }
- } else {
- // use original dimensions
- $width = $info[0];
- $height = $info[1];
- }
-
- // create new image to hold thumbnail
- $im = ImageCreate($width, $height);
-
- /* load original image, depending on type - see GetImageSize note
- * above for description of file types
- */
- switch ($info[2]) {
- case 2 : $im_big = ImageCreateFromJPEG("$file");
- break;
- case 3 : $im_big = ImageCreateFromPNG("$file");
- break;
- default: $im_big = "";
- }
-
- // if image couldn't be loaded, create a 'no thumbnail avail' image instead
- if (!$im_big) {
- $width = 100;
- $height = 100;
- $im = ImageCreate($width, $height); // create blank image
- $bgc = ImageColorAllocate($im, 0, 0, 0); // background color black
- $tc = ImageColorAllocate($im, 255, 255, 255); // text color white
- ImageFilledRectangle($im, 0, 0, $width, $height, $bgc); // fill in image
- ImageString($im, 3, 9, 36, "No thumbnail", $tc); // write text
- ImageString($im, 3, 17, 48, "available", $tc);
-
- // otherwise, resize original image into thumbnail
- } else {
- ImageCopyResized($im,$im_big,0,0,0,0,$width,$height,$info[0],$info[1]);
- }
-
- // output the PNG image to the browser
- ImagePNG($im);
-
- // clean up
- ImageDestroy($im_big);
- ImageDestroy($im);
-
- ?>